/**
 * HTTP请求模块
 * 
 * 统一管理项目中的所有API请求和网络配置
 */

import axios from './axios'
import { authAPI, dataAPI, fileAPI, initLeanCloud } from './leancloud'
import { 
  LC_APP_ID, 
  LC_APP_KEY, 
  LC_SERVER_URL, 
  getRequestHeaders, 
  getMasterKeyHeaders, 
  getSessionHeaders 
} from './config'

// 导出配置信息和LeanCloud相关API
export { 
  LC_APP_ID, 
  LC_APP_KEY, 
  LC_SERVER_URL, 
  getRequestHeaders, 
  getMasterKeyHeaders, 
  getSessionHeaders,
  authAPI, 
  dataAPI, 
  fileAPI, 
  initLeanCloud 
}

// 通用API请求方法
const request = {
  get: <T>(url: string, params?: any) => axios.get<T, T>(url, { params }),
  post: <T>(url: string, data?: any) => axios.post<T, T>(url, data),
  put: <T>(url: string, data?: any) => axios.put<T, T>(url, data),
  delete: <T>(url: string) => axios.delete<T, T>(url)
}

/**
 * 业务模块API
 * 可以根据不同业务模块划分API
 */

// 订单相关API
export const orderAPI = {
  // 获取订单列表
  getOrders: (params?: any) => dataAPI.query('Order', params),
  
  // 获取订单详情
  getOrderDetail: (orderId: string) => dataAPI.get('Order', orderId),
  
  // 创建订单
  createOrder: (orderData: any) => dataAPI.save('Order', orderData),
  
  // 更新订单
  updateOrder: (orderData: any) => {
    if (!orderData.objectId) {
      throw new Error('更新订单时必须提供objectId');
    }
    return dataAPI.save('Order', orderData);
  },
  
  // 删除订单
  deleteOrder: (orderId: string) => dataAPI.delete('Order', orderId)
}

// 车辆管理相关API
export const vehicleAPI = {
  // 获取车辆列表
  getVehicles: (params?: any) => dataAPI.query('Vehicle', params),
  
  // 获取车辆详情
  getVehicleDetail: (vehicleId: string) => dataAPI.get('Vehicle', vehicleId),
  
  // 创建车辆
  createVehicle: (vehicleData: any) => dataAPI.save('Vehicle', vehicleData),
  
  // 更新车辆
  updateVehicle: (vehicleData: any) => {
    if (!vehicleData.objectId) {
      throw new Error('更新车辆时必须提供objectId');
    }
    return dataAPI.save('Vehicle', vehicleData);
  },
  
  // 删除车辆
  deleteVehicle: (vehicleId: string) => dataAPI.delete('Vehicle', vehicleId)
}

// 司机管理相关API
export const driverAPI = {
  // 获取司机列表
  getDrivers: (params?: any) => dataAPI.query('Driver', params),
  
  // 获取司机详情
  getDriverDetail: (driverId: string) => dataAPI.get('Driver', driverId),
  
  // 创建司机
  createDriver: (driverData: any) => dataAPI.save('Driver', driverData),
  
  // 更新司机
  updateDriver: (driverData: any) => {
    if (!driverData.objectId) {
      throw new Error('更新司机时必须提供objectId');
    }
    return dataAPI.save('Driver', driverData);
  },
  
  // 删除司机
  deleteDriver: (driverId: string) => dataAPI.delete('Driver', driverId)
}

// 客户管理相关API
export const customerAPI = {
  // 获取客户列表
  getCustomers: (params?: any) => dataAPI.query('Customer', params),
  
  // 获取客户详情
  getCustomerDetail: (customerId: string) => dataAPI.get('Customer', customerId),
  
  // 创建客户
  createCustomer: (customerData: any) => dataAPI.save('Customer', customerData),
  
  // 更新客户
  updateCustomer: (customerData: any) => {
    if (!customerData.objectId) {
      throw new Error('更新客户时必须提供objectId');
    }
    return dataAPI.save('Customer', customerData);
  },
  
  // 删除客户
  deleteCustomer: (customerId: string) => dataAPI.delete('Customer', customerId)
}

// 站点管理相关API
export const stationAPI = {
  // 获取站点列表
  getStations: (params?: any) => dataAPI.query('Station', params),
  
  // 获取站点详情
  getStationDetail: (stationId: string) => dataAPI.get('Station', stationId),
  
  // 创建站点
  createStation: (stationData: any) => dataAPI.save('Station', stationData),
  
  // 更新站点
  updateStation: (stationData: any) => {
    if (!stationData.objectId) {
      throw new Error('更新站点时必须提供objectId');
    }
    return dataAPI.save('Station', stationData);
  },
  
  // 删除站点
  deleteStation: (stationId: string) => dataAPI.delete('Station', stationId)
}

// 运单管理相关API
export const waybillAPI = {
  // 获取运单列表
  getWaybills: (params?: any) => dataAPI.query('Waybill', params),
  
  // 获取运单详情
  getWaybillDetail: (waybillId: string) => dataAPI.get('Waybill', waybillId),
  
  // 创建运单
  createWaybill: (waybillData: any) => dataAPI.save('Waybill', waybillData),
  
  // 更新运单
  updateWaybill: (waybillData: any) => {
    if (!waybillData.objectId) {
      throw new Error('更新运单时必须提供objectId');
    }
    return dataAPI.save('Waybill', waybillData);
  },
  
  // 删除运单
  deleteWaybill: (waybillId: string) => dataAPI.delete('Waybill', waybillId),
  
  // 根据订单ID获取运单
  getWaybillsByOrderId: (orderId: string) => {
    return dataAPI.query('Waybill', {
      where: { order: { __type: 'Pointer', className: 'Order', objectId: orderId } }
    });
  }
}

// 账单管理相关API
export const billAPI = {
  // 获取账单列表
  getBills: (params?: any) => dataAPI.query('Bill', params),
  
  // 获取账单详情
  getBillDetail: (billId: string) => dataAPI.get('Bill', billId),
  
  // 创建账单
  createBill: (billData: any) => dataAPI.save('Bill', billData),
  
  // 更新账单
  updateBill: (billData: any) => {
    if (!billData.objectId) {
      throw new Error('更新账单时必须提供objectId');
    }
    return dataAPI.save('Bill', billData);
  },
  
  // 删除账单
  deleteBill: (billId: string) => dataAPI.delete('Bill', billId)
}

// 导出通用请求方法
export { request }

// 默认导出所有API
export default {
  auth: authAPI,
  order: orderAPI,
  vehicle: vehicleAPI,
  driver: driverAPI,
  customer: customerAPI,
  station: stationAPI,
  waybill: waybillAPI,
  bill: billAPI,
  file: fileAPI
} 